package com.example.teesystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.teesystem.entity.ExamPaper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
* @author NieYaShi
* @description 针对表【t_exam_paper】的数据库操作Mapper
* @createDate 2022-04-11 09:14:07
* @Entity com.example.teesystem.entity.ExamPaper
*/
@Mapper
public interface ExamPaperMapper extends BaseMapper<ExamPaper> {

    @Update("update t_exam_paper set is_publish = 1 where id = #{id}")
    void releaseExamPaper(int id);

    @Update("update t_exam_paper set is_delete = 1 where id = #{id}")
    void deleteExamPaper(Integer id);

    @Update("update t_exam_paper set is_publish = 0 where id = #{id}")
    void undoExamPaper(Integer id);

    @Select("select name from t_exam_paper where id = #{id} and is_delete = 0")
    String getNameById(int id);

    @Update("update t_exam_paper set score = #{score} where id = #{id} and is_delete = 0")
    void updateExamPaperScore(Integer id, Integer score);

    @Select("select * from t_exam_paper where course_id = #{courseId} and id = #{paperId} and is_mark = 1 and is_delete = 0")
    ExamPaper getPaperByCourseIdAndId(int courseId, int paperId);

    @Select("select course_id from t_exam_paper where id = #{paperId}")
    Integer getCourseIdByPaperId(int paperId);

    @Select("select * from t_exam_paper where course_id = #{courseId} and is_delete = 0")
    List<ExamPaper> getExamPaperByCourseId(int courseId);

    @Select("select * from t_exam_paper where is_publish = 1 and is_finish = 1 and is_mark = 1 order by end_time desc")
    List<ExamPaper> getHistoryExamPaper();

    @Select("select * from t_exam_paper where course_id = #{courseId} and is_publish = 1 and is_finish = 1 and is_mark = 1 and is_delete = 0")
    List<ExamPaper> getMarkedExamPaperByCourseId(int courseId);

    @Update("update t_exam_paper set include_issue_type = #{includeIssueType} where id = #{paperId}")
    void updateIncludeIssueType(int paperId, String includeIssueType);

    @Select("SELECT * from t_exam_paper WHERE is_publish = 1 AND is_finish = 1 AND is_delete = 0 AND course_id = #{courseId} and classes REGEXP #{classIdRegexp}")
    List<ExamPaper> getMarkedPaperByCourseAndClassId(int courseId, String classIdRegexp);

    @Select("SELECT * from t_exam_paper WHERE is_publish = 1 AND is_finish = 1 AND is_mark = 0 AND is_delete = 0 AND course_id = #{courseId} and classes REGEXP #{classIdRegexp}")
    List<ExamPaper> getNoMarkedPaperByCourseAndClassId(int courseId, String classIdRegexp);

    @Select("SELECT id from t_exam_paper WHERE is_publish = 1 AND is_finish = 0 AND is_mark = 0 AND is_delete = 0 and classes REGEXP #{classIdRegexp}")
    List<Integer> getExaminationPaperIds(String classIdRegexp);

    @Select("select id from t_exam_paper where is_delete = 0 and classes REGEXP #{classIdRegexp}")
    List<Integer> getPaperIdByClassId(String classIdRegexp);

    @Select("select * from t_exam_paper where is_publish = 1 and is_finish = 0 and is_delete = 0 and end_time < #{now}")
    List<ExamPaper> updateExamPaper(LocalDateTime now);

    @Update("update t_exam_paper set is_finish = 1 where id = #{id}")
    void finishExam(int id);
}




